package com.kakarote.crm9.erp.jxc.service;

import com.jfinal.plugin.activerecord.Db;
import com.kakarote.crm9.erp.jxc.entity.ProductInfoEntity;
import com.kakarote.crm9.utils.R;

import java.util.List;

/**
 * @Author:Lzq
 * @Date:2021/3/15
 */
public class ProductInfoService {

    public List<ProductInfoEntity> getGoodsCategoryList(String startNo, String pageSize) {

        List<ProductInfoEntity> list = ProductInfoEntity.dao.find("select * from product_info order by id desc limit "+startNo+","+pageSize);

        return list;
    }

    public R addProduct(ProductInfoEntity productInfo) {

        boolean flag = productInfo.save();
        return flag?R.ok(): R.error();
    }

    public ProductInfoEntity getInfoById(Integer id) {
        ProductInfoEntity product = ProductInfoEntity.dao.findById(id);
        return product;
    }

    public R updateProduct(ProductInfoEntity model) {
        boolean flag = model.update();
        return flag ? R.ok() : R.error();
    }

    public R delProduct(Integer id) {
        boolean flag = ProductInfoEntity.dao.deleteById(id);
        return flag ? R.ok() : R.error();
    }

    public List<ProductInfoEntity> getGoodsCategoryListByCondition(String name, String startNo, String pageSize) {
        return ProductInfoEntity.dao.find("select * from product_info where name like '%"+name+"%' order by id desc limit "+startNo+","+pageSize);
    }

    public List<ProductInfoEntity> getGoodsCategoryListByCondition(Integer belongCategory, String startNo, String pageSize) {


        return ProductInfoEntity.dao.find("select * from product_info where belong_category = "+ belongCategory +" order by id desc limit "+startNo+","+pageSize);
    }

    public List<ProductInfoEntity> getGoodsCategoryListByCondition(String name ,Integer belongCategory, String startNo, String pageSize) {
        return ProductInfoEntity.dao.find("select * from product_info where name like concat('%"+ name+"%') and belong_category = "+belongCategory+" order by id desc limit "+startNo+","+pageSize);
    }

    public Integer getTotal(String name) {
        return Db.find("select id from product_info where name like concat('%"+ name+"%')").size();
    }

    public Integer getTotal(Integer belongCategory) {
        return Db.find("select id from product_info where belong_category = "+belongCategory).size();
    }

    public Integer getTotal(String name, Integer belongCategory) {
        return Db.find("select id from product_info where name like concat('%"+ name+"%') and belong_category = "+belongCategory).size();
    }

    public Integer getTotal() {
        return Db.find("select id from product_info").size();
    }
}
